package com.kotlin.android.app

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.facade.annotation.Route
import com.android.app.router.ext.AppNavigationExt
import com.android.app.router.path.RouterActivityPath
import com.kotlin.android.app.data.common.Constant.KEY_TITLE
import com.kotlin.android.app.databinding.ActMainTabBinding
import com.kotlin.android.app.ktx.ext.showToast
import com.kotlin.android.app.tab.TabBarItem
import com.kotlin.android.app.ui.fragment.CaiFuFragment
import com.kotlin.android.app.ui.fragment.HuoDongFragment
import com.kotlin.android.app.ui.fragment.ShouYeFragment
import com.kotlin.android.app.ui.fragment.WoDeFragment
import com.kotlin.android.app.ui.fragment.ZiXunFragment
import com.kotlin.android.core.BaseVMActivity
import com.kotlin.android.core.entity.PAGE_FLAG
import com.kotlin.android.core.entity.PageFlag
import com.kotlin.android.ktx.ext.core.getCompatDrawable
import com.kotlin.android.ktx.ext.core.put
import com.kotlin.android.ktx.ext.log.e
import com.kotlin.android.ktx.lifecycle.KtxActivityManager

import com.kotlin.android.widget.dialog.apk.ApkUpgradeDialog
import com.kotlin.android.widget.tips.TipsPopupWindow

/**
 * Tab 主页
 *
 */
@Route(path = RouterActivityPath.App.PAGER_APP)
class MainTabActivity : BaseVMActivity<AppViewModel, ActMainTabBinding>() {
    private var apkUpgradeDialog: ApkUpgradeDialog? = null
    private var pageFlag: PageFlag? = null
    private var isNewIntent: Boolean = false

    //记录用户首次点击返回键的时间
    private var firstTime: Long = 0

    private var homeTag = "home_frag"
    private var filmTag = "film_frag"
    private var cornTag = "corn_frag"
    private var mallTag = "mall_frag"
    private var mineTag = "mine_frag"
    private var homeFragment: Fragment? = null
    private var filmFragment: Fragment? = null
    private var cornFragment: Fragment? = null
    private var mallFragment: Fragment? = null
    private var mineFragment: Fragment? = null

    /**
     * 到账提醒和过期提醒PopupWindow
     */
    private var tipsPop: TipsPopupWindow? = null

    override fun initRecover(savedInstanceState: Bundle) {
        super.initRecover(savedInstanceState)
        homeFragment = supportFragmentManager.findFragmentByTag(homeTag)
        filmFragment = supportFragmentManager.findFragmentByTag(filmTag)
        cornFragment = supportFragmentManager.findFragmentByTag(cornTag)
        mallFragment = supportFragmentManager.findFragmentByTag(mallTag)
        mineFragment = supportFragmentManager.findFragmentByTag(mineTag)
    }

    override fun getIntentData(intent: Intent?) {
        super.getIntentData(intent)
        intent?.apply {
            pageFlag = getParcelableExtra(PAGE_FLAG)
        }
        openAppByScheme(null)
    }

    override fun initNewData() {
        super.initNewData()
        isNewIntent = true
    }

    override fun initVB(): ActMainTabBinding? {
        return try {
            super.initVB()
        } catch (e: Exception) {
            e.printStackTrace()
            "${javaClass.simpleName} initVB: (ActMainTabBinding) 异常".e()
            ActMainTabBinding.inflate(layoutInflater)
        }
    }

    override fun initView() {
        mBinding?.tabBar?.apply {
            init(container = R.id.container, fragmentManager = supportFragmentManager)
            addItem(
                TabBarItem(
                    context = this@MainTabActivity,
                    tag = homeTag,
                    fragment = homeFragment ?: CaiFuFragment().apply {
                        arguments = Bundle().apply {
                            put(KEY_TITLE, this@MainTabActivity.getString(R.string.tab_name_caifu))
                        }
                        homeFragment = this
                    },
                    titleRes = R.string.tab_name_caifu,
                    iconDrawable = getCompatDrawable(R.drawable.ic_caifu_btn_n),
                    iconDrawableHighlight = getCompatDrawable(R.drawable.ic_caifu_btn_h),
                )
            )
            addItem(
                TabBarItem(
                    context = this@MainTabActivity,
                    tag = filmTag,
                    fragment = filmFragment ?: ZiXunFragment().apply {
                        arguments = Bundle().apply {
                            put(
                                KEY_TITLE,
                                this@MainTabActivity.getString(R.string.tab_name_zixun)
                            )
                        }
                        filmFragment = this
                    },
                    titleRes = R.string.tab_name_zixun,
                    iconDrawable = getCompatDrawable(R.drawable.ic_zixun_btn_n),
                    iconDrawableHighlight = getCompatDrawable(R.drawable.ic_zixun_btn_h),
                )
            )

            addItem(
                TabBarItem(
                    context = this@MainTabActivity,
                    tag = mallTag,
                    fragment = mallFragment ?: ShouYeFragment().apply {
                        mallFragment = this
                    },
                    titleRes = R.string.tab_name_home,
                    iconDrawable = getCompatDrawable(R.drawable.ic_shouye_btn_n),
                    iconDrawableHighlight = getCompatDrawable(R.drawable.ic_shouye_btn_h),

                )
            )
            addItem(
                TabBarItem(
                    context = this@MainTabActivity,
                    tag = cornTag,
                    fragment = cornFragment ?: HuoDongFragment().apply {
                        arguments = Bundle().apply {
                            put(
                                KEY_TITLE,
                                this@MainTabActivity.getString(R.string.tab_name_huodong)
                            )
                        }
                        cornFragment = this
                    },
                    titleRes = R.string.tab_name_huodong,
                    iconDrawable = getCompatDrawable(R.drawable.ic_huodong_btn_n),
                    iconDrawableHighlight = getCompatDrawable(R.drawable.ic_huodong_btn_h)
                )
            )
            addItem(
                TabBarItem(
                    context = this@MainTabActivity,
                    tag = mineTag,
                    fragment = mineFragment ?: WoDeFragment().apply {
                        arguments = Bundle().apply {
                            put(KEY_TITLE, this@MainTabActivity.getString(R.string.tab_name_person))
                        }
                        mineFragment = this
                    },
                    titleRes = R.string.tab_name_person,
                    iconDrawable = getCompatDrawable(R.drawable.ic_wode_btn_n),
                    iconDrawableHighlight = getCompatDrawable(R.drawable.ic_wode_btn_h),
                )
            )
            action = {
                if (it == 3) {
//                    if(!isLogin()){
//                        RouterManager.instance.getProvider(IUserProvider::class.java)
//                            ?.startLoginPage(context = this@MainTabActivity)
//                    }
                }



            }
            applyPageFlag(pageFlag)
        }
        AppNavigationExt.openMainTab(2)

    }

    override fun initData() {
//        if (isLogin()) {
//            mViewModel?.getRefreshToken()
//        }
//        mViewModel?.loadAppService(count = 2)
    }

    override fun startObserve() {
    }


    override fun onResume() {
        super.onResume()
        if (isNewIntent) {
            isNewIntent = false
            mBinding?.tabBar?.applyPageFlag(pageFlag)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        tipsPop?.destroy()
    }

    override fun onBackPressed() {
        val secondTime = System.currentTimeMillis()
        if (secondTime - firstTime > 2000) {//间隔大于2s
            showToast("再按一次退出app")
            firstTime = secondTime
        } else {
            super.onBackPressed()
            KtxActivityManager.exitApp()
        }
    }
}